/**
 *
 */
if(typeof (module) !="undefined"){
    module.exports = FColor; //

    var FEffectBase = require('./FEffectBase');
    var FColorBase = require('./FColorBase');
    var FColorConverter = require('./FColorConverter');
}



function FColor(obj)
{
    FColorBase.call(this);
    this.type = FEffectBase.prototype.TYPE_COLOR;

    // 颜色值
    if(typeof obj==undefined || typeof  obj=="undefined"){
        this.C = 0.0; // 范围：[0.00%,10.00%]
        this.M = 0.0;
        this.Y = 0.0;
        this.K = 0.0;
    }else{
        this.C = obj.C;
        this.M = obj.M;
        this.Y = obj.Y;
        this.K = obj.K;
    }

}
FColor.prototype = new FColorBase();


// 从json对象(注意不是json字符串)中解析
FColor.prototype.fromJson = function(jsonObj, versionId){
    FColorBase.prototype.fromJson.call(this,jsonObj,versionId);

    this.C = jsonObj.C;
    this.M = jsonObj.M;
    this.Y = jsonObj.Y;
    this.K = jsonObj.K;
}
// 转换成json字符串
FColor.prototype.toJson = function(){
    // 去掉父json字符串的头尾
    var superJson = FColorBase.prototype.toJson.call(this);
    superJson = superJson.substr(1,superJson.length-2);

    var ret = '{';
    ret +=superJson;

    // C
    ret +=',';
    ret += '"C":';
    ret += this.C;
    // M
    ret +=',';
    ret += '"M":';
    ret += this.M;
    // Y
    ret +=',';
    ret += '"Y":';
    ret += this.Y;
    // K
    ret +=',';
    ret += '"K":';
    ret += this.K;



    ret+='}';
    return ret;
}
// 转换成PDFKit识别的RGB Array
FColor.prototype.toPDFKitRGBArray = function(){
    var rgb = FColorConverter.prototype.CMYK_2_RGB(this.C, this.M, this.Y, this.K);
    return rgb;
}
// 转换成PDFKit识别的CMYK Array
FColor.prototype.toPDFKitCMYKArray = function(){
    // PDFKit 的CMYK的值范围是[0,100],而我们记录的范围是[0.00,1.00]， 需要转换
    return [this.C*100,this.M*100,this.Y*100,this.K*100];
}
//  把数字转换成两位十六进制
FColor.prototype.toTwoHexStr = function(n){
    var hex = n.toString(16);
    var ret="";
    if(hex==null|| hex==0){
        ret ="00";
    }else if(hex.length==1){
        ret = "0" + hex;
    }else if(hex.length==2){
        ret = hex;
    }else{
        ret = hex.substr(0,2);
    }
    return ret;
}
// 转换成imagemagic识别的RGB String
// 例如：FColor 的toRGB结果是0（rgb分量都为0），则转换成 "#000000"
// 例如：FColor 的toRGB结果是11189196（rgb分量分别为AA,BB,CC）， "#AABBCC"
FColor.prototype.toFormatRGBStr = function(){
    var rgb = FColorConverter.prototype.CMYK_2_RGB(this.C, this.M, this.Y, this.K);
    var r = FColor.prototype.toTwoHexStr(rgb[0]);
    var g = FColor.prototype.toTwoHexStr(rgb[1]);
    var b = FColor.prototype.toTwoHexStr(rgb[2]);

    return '#'+r+g+b;
}
// 转换成RGB
FColor.prototype.toRGB = function(){
    var rgb = FColorConverter.prototype.CMYK_2_RGB(this.C, this.M, this.Y, this.K);
    return (rgb[0]<<16)|(rgb[1]<<8)|rgb[2];
}
// 从RGB生成
FColor.prototype.fromRGB = function(rgb){
    var cmyk = FColorConverter.prototype.RGB_2_CMYK((rgb&0xFF0000)>>16, (rgb&0x00FF00)>>8, rgb&0x0000FF);
    this.C = cmyk[0];
    this.M = cmyk[1];
    this.Y = cmyk[2];
    this.K = cmyk[3];
}

// 根据rgb创建实例
FColor.prototype.createFromRGB = function(rgb){
    var ret = new FColor();
    ret.fromRGB(rgb);
    return ret;
}
